package com.zxbit.flash.utils;

import android.graphics.Bitmap;

import com.zxbit.flash.common.ImageConstant;

/**
 * @author lixiaonan
 * 功能描述: 项目中使用的图片上传的工具类
 * 时 间： 2019/3/21 下午3:29
 */
public class UploadImageUtil {
    /**
     * 生成图片上传的bitmap对象这个bitmap要保存到本地才起作用
     *
     * @param bitmap
     * @return
     */
    public static Bitmap getUploadNeedBitmap(Bitmap bitmap) {
        bitmap = ImageUtils.compressBySampleSize(bitmap, ImageConstant.UPLOAD_MAX_WIDTH_SIZE,
                ImageConstant.UPLOAD_MAX_HEIGHT_SIZE,true);
        Bitmap result = ImageUtils.compressByQuality(bitmap, ImageConstant
                .UPLOAD_FILE_MAX_LENGTH, true);
        return result;
    }
    /**
     * 通过大小来改变文件大小
     * @param bitmap
     * @return
     */
    public static Bitmap getUploadNeedBitmapBYSize(Bitmap bitmap) {
        bitmap = ImageUtils.compressBySampleSize(bitmap, ImageConstant.UPLOAD_MAX_WIDTH_SIZE,
                ImageConstant.UPLOAD_MAX_HEIGHT_SIZE,false);
//        int sampleSize=1;
        while (bitmap.getAllocationByteCount()>ImageConstant
                .UPLOAD_FILE_MAX_LENGTH){
//            sampleSize = sampleSize << 1;
            bitmap=ImageUtils.compressBySampleSize(bitmap,2,true);
        }
        return bitmap;
    }

    /**
     * 计算合适的大小
     * @return
     */
    public static int[] getNeedSize(int width,int height){
        int[] needsize=new int[2];
        if(width>height){
            needsize[1]=ImageConstant.UPLOAD_MIN_HEIGHT_SIZE+4;
            needsize[0]=ImageConstant.UPLOAD_MIN_HEIGHT_SIZE/height*width;
        }else{
            needsize[0]=ImageConstant.UPLOAD_MIN_WIDTH_SIZE+4;
            needsize[1]= (int) (ImageConstant.UPLOAD_MIN_WIDTH_SIZE/(double)width*height);
        }
        return needsize;
    }
    /**
     * 通过图片尺寸大小来检查
     * @param bitmap
     * @return
     */
    public static boolean checkBitmapSize(Bitmap bitmap) {
        if(null!=bitmap){
            try {
                int width=bitmap.getWidth();
                int height=bitmap.getHeight();
                if(width>ImageConstant.UPLOAD_MIN_WIDTH_SIZE&&height>ImageConstant.UPLOAD_MIN_HEIGHT_SIZE&&
                        width<ImageConstant.UPLOAD_MAX_WIDTH_SIZE&& height<ImageConstant.UPLOAD_MAX_HEIGHT_SIZE ){
                    return true;
                }
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
        return false;
    }


    public static void main(String[] args) {
        int a=1;
        while (a<12){
            a= a << 1;
            System.out.println(a);
        }
    }
}
